package com.design.composite.service.impl;

import com.design.composite.bean.ResponseResult;
import com.design.composite.entity.Material;
import com.design.composite.repository.DictRepository;
import com.design.composite.repository.MaterialRepository;
import com.design.composite.service.IMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author liuyajun
 */
@Service
public class MaterialServiceImpl implements IMaterialService {

    @Autowired
    private MaterialRepository materialRepository;
    @Autowired
    private DictRepository dictRepository;

    @Override
    public ResponseResult delete(Integer id) {
        Material one = materialRepository.getOne(id);
        String filePath = one.getFilePath();
        String materialPrePath = dictRepository.findByCode("MATERIAL").getPath();
        File file = new File(materialPrePath + filePath);
        file.delete();
        materialRepository.delete(one);
        return ResponseResult.success(null);
    }

    @Override
    public List<Material> findList(Integer classifyId) {
        List<Material> underClass = materialRepository.findByClassifyId(classifyId);
        if (underClass != null && underClass.size() > 0) {
            return underClass;
        }
        return new ArrayList<>();
    }

    @Override
    public ResponseResult add(Material material) {
        Material has = materialRepository.findByMaterialNameAndClassifyId(material.getMaterialName(), material.getClassifyId());
        if (has != null) {
            boolean flag = true;
            if (material.getId() == null) {
                flag = false;
            } else {
                if (has.getId().intValue() != material.getId().intValue()) {
                    flag = false;
                }
            }
            if (flag == false) {
                return ResponseResult.fail("该分类下已包含同名素材,不可重复创建！");
            }
        }
        materialRepository.save(material);
        return ResponseResult.success(null);
    }

    @Override
    public ResponseResult uploadFile(MultipartFile file) {
        //获取文件上传所要保存的"文件夹路径"
        String realPath = dictRepository.findByCode("MATERIAL").getPath();
        //存放上传文件的文件夹
        File parentDir = new File(realPath);
        if (!parentDir.isDirectory()) {
            //递归生成文件夹
            parentDir.mkdirs();
        }
        //获取原文件名
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            //构建真实的文件路径
            File newFile = new File(parentDir.getAbsolutePath() + File.separator + newName);
            //转存文件到指定路径，如果文件名重复的话，将会覆盖掉之前的文件,这里是把文件上传到 “绝对路径”
            file.transferTo(newFile);
            return ResponseResult.success(newName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.fail("上传失败！");
    }

}

